using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Shuriken.Core.Ranking;

namespace Shuriken.Core.UI.StandardUI
{
    public partial class InputItemView : UserControl
    {
        private int m_ItemCount;
        private frmMain m_ParentForm;
        private bool m_ControlSelected;
        private bool m_TextEntryMode; 

        private string ImageKey = string.Empty; 
        // private Image img = null; 

        public InputItemView()
        {
            InitializeComponent();

            SearchCount = 0;
            SearchTerm = string.Empty;
            txtText.Visible = false;
        }

        /// <summary>
        /// Search Term for this control
        /// </summary>
        public string SearchTerm
        {
            get
            {
                return lblSearchTerm.Text;
            }

            set
            {
                lblSearchTerm.Text = value; 
            }
        }

        /// <summary>
        /// Number of items displayed by this control
        /// </summary>
        public int SearchCount
        {
            get
            {
                return m_ItemCount; 
            }

            set
            {
                m_ItemCount = value;
                UpdateItemCount(m_ItemCount);
            }
        }

        /// <summary>
        /// Updates the label to show the number of items
        /// </summary>
        /// <param name="m_ItemCount"></param>
        private void UpdateItemCount(int m_ItemCount)
        {
            if (m_ItemCount <= 0)
            {
                lblNumberItem.Text = "No Items";
            }
            else if (m_ItemCount == 1)
            {
                lblNumberItem.Text = "1 Item";
            }
            else
            {
                lblNumberItem.Text = m_ItemCount + " Items"; 
            }
        }

        /// <summary>
        /// Gets or Sets if the control is selected or not. 
        /// </summary>
        public bool ControlSelected
        {
            get
            {
                return m_ControlSelected; 
            }

            set
            {
                m_ControlSelected = value;
                ControlSelectedChanged(m_ControlSelected);
            }
        }

        /// <summary>
        /// Changes the display of the control to selected or non selected. 
        /// </summary>
        /// <param name="m_ControlSelected">control selected</param>
        private void ControlSelectedChanged(bool m_ControlSelected)
        {
            if (m_ControlSelected)
            {
                // Selected
                detailsPanel.GradientHighColor = System.Drawing.Color.Linen;
                detailsPanel.GradientLowColor = System.Drawing.Color.Orange;                
                detailsPanel.BorderColor = new Ascend.BorderColor(Color.Orange);
            }
            else
            {
                // UnSelected 
                detailsPanel.GradientHighColor = System.Drawing.Color.White;
                detailsPanel.GradientLowColor = System.Drawing.Color.LightSteelBlue;
                detailsPanel.BorderColor = new Ascend.BorderColor(Color.Gray);
                //detailsPanel.BorderColor = new Ascend.BorderColor(Color.LightSteelBlue);
            }
        }

        /// <summary>
        /// Shows the display item on the control 
        /// </summary>
        /// <param name="SelectedDisplayItem">Item to display.</param>
        /// <param name="SearchKey">Search key</param>
        /// <param name="ItemCount">Item count</param>
        public void ShowDisplayItem(Shuriken.Core.Store.ISearchable DisplayItem, string SearchKey, int ItemCount)
        {
            System.Diagnostics.Trace.Assert(m_ParentForm != null);
            System.Diagnostics.Trace.Assert(DisplayItem != null);

            SearchTerm = SearchKey;
            SearchCount = ItemCount;

            lbDisplaylItemDescription.Text = DisplayItem.Description;
            lblDisplayItemName.Text = DisplayItem.Name;
            lblDisplayItemName.HitMask = DisplayItem.HitMask;

            ImageKey = DisplayItem.Icon;
            IconPanel.Invalidate();
        }

        private void IconPanel_Paint(object sender, PaintEventArgs e)
        {
            if (!String.IsNullOrEmpty(ImageKey))
            {
                Store.ImageStore.DrawImage(e.Graphics, 1, 1, 32, 32, ImageKey);
            }
        }

        /// <summary>
        /// Registers the parent form.
        /// </summary>
        /// <param name="ParentForm">Parent form.</param>
        internal void RegisterParentForm(frmMain ParentForm)
        {
            m_ParentForm = ParentForm; 
        }

        /// <summary>
        /// For activating and querying text entry mode 
        /// </summary>
        public bool TextEntryMode
        {
            get
            {
                return m_TextEntryMode;
            }
            set
            {
                ChangeTextEntryMode(value);
            }
        }

        /// <summary>
        /// Text for text entry mode 
        /// </summary>
        public string TextEntryText
        {
            get
            {
                return txtText.Text;
            }
            set
            {
                txtText.Text = value;
            }
        }

        private void ChangeTextEntryMode(bool value)
        {
            if (value == m_TextEntryMode)
            {
                return; 
            }

            if (value)
            {
                // in text entry mode
                txtText.SelectAll();
                txtText.Visible = true;
                txtText.Focus();                
            }
            else
            {
                // leaving text entry mode 
                txtText.Visible = false;
            }

            m_TextEntryMode = value; 
        }
    }
}
